/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.nxu.campus.zhxy.service.impl;

import com.nxu.campus.utils.FileUtil;
import com.nxu.campus.utils.PageUtil;
import com.nxu.campus.utils.QueryHelp;
import com.nxu.campus.utils.ValidationUtil;
import com.nxu.campus.zhxy.domain.ZhxyStudent;
import com.nxu.campus.zhxy.repository.ZhxyStudentRepository;
import com.nxu.campus.zhxy.service.ZhxyStudentService;
import com.nxu.campus.zhxy.service.dto.ZhxyStudentDto;
import com.nxu.campus.zhxy.service.dto.ZhxyStudentQueryCriteria;
import com.nxu.campus.zhxy.service.mapstruct.ZhxyStudentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HWL
* @date 2022-04-15
**/
@Service
@RequiredArgsConstructor
public class ZhxyStudentServiceImpl implements ZhxyStudentService {

    private final ZhxyStudentRepository zhxyStudentRepository;
    private final ZhxyStudentMapper zhxyStudentMapper;

    @Override
    public Map<String,Object> queryAll(ZhxyStudentQueryCriteria criteria, Pageable pageable){
        Page<ZhxyStudent> page = zhxyStudentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zhxyStudentMapper::toDto));
    }

    @Override
    public List<ZhxyStudentDto> queryAll(ZhxyStudentQueryCriteria criteria){
        return zhxyStudentMapper.toDto(zhxyStudentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ZhxyStudentDto findById(String studentId) {
        ZhxyStudent zhxyStudent = zhxyStudentRepository.findById(studentId).orElseGet(ZhxyStudent::new);
        ValidationUtil.isNull(zhxyStudent.getStudentId(),"ZhxyStudent","studentId",studentId);
        return zhxyStudentMapper.toDto(zhxyStudent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZhxyStudentDto create(ZhxyStudent resources) {
//        resources.setStudentId(IdUtil.simpleUUID());
        return zhxyStudentMapper.toDto(zhxyStudentRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ZhxyStudent resources) {
        ZhxyStudent zhxyStudent = zhxyStudentRepository.findById(resources.getStudentId()).orElseGet(ZhxyStudent::new);
        ValidationUtil.isNull( zhxyStudent.getStudentId(),"ZhxyStudent","id",resources.getStudentId());
        zhxyStudent.copy(resources);
        zhxyStudentRepository.save(zhxyStudent);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String studentId : ids) {
            zhxyStudentRepository.deleteById(studentId);
        }
    }

    @Override
    public void download(List<ZhxyStudentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZhxyStudentDto zhxyStudent : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("年级", zhxyStudent.getGrade());
            map.put("专业", zhxyStudent.getMajor().getName());
            map.put("班级", zhxyStudent.getClasse().getName());
            map.put("院系", zhxyStudent.getDept().getName());
            map.put("姓名", zhxyStudent.getStudentName());
            map.put("性别", zhxyStudent.getGender());
            map.put("民族", zhxyStudent.getEthnic());
            map.put("身份证号", zhxyStudent.getIdno());
            map.put("出生日期", zhxyStudent.getDofb());
            map.put("手机号", zhxyStudent.getPhone());
            map.put("邮箱", zhxyStudent.getEmail());
            map.put("地址", zhxyStudent.getAddress());
            map.put("学籍状态", zhxyStudent.getAtatus());
            map.put("入校时间", zhxyStudent.getEnrollmenttime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
